<?php

/* **************************************************************************
File:					rss.php
EstateAgent Improved - Joomla Component
Year: 2007
Author: Manos Orfanoudakis - "vosk"
Homepage: 		http://www.eaimproved.eu
Version: 			1.3.x                     
License:			GNU/GPL http://www.gnu.org/copyleft/gpl.html
*************************************************************************** */


defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

// set the file content type and character set
header("Content-Type: text/xml; charset=UTF-8");

$today = date('Y-m-d');

$query = " SELECT * FROM #__estateagent WHERE "
		 . " (((publish_up >= '0000-00-00' AND publish_up <= '$today') AND publish_down >= '$today') "
	    . " OR ((publish_up >= '0000-00-00' AND publish_up <= '$today' ) AND (publish_down = '0000-00-00' OR publish_down >= '$today')) "
		 . " OR (publish_up = '0000-00-00' AND publish_down = '0000-00-00')) "
	    . " AND published = 1 AND block = 0 AND older = 0 AND is_agent = 0 ORDER BY timestamp DESC LIMIT 10 ";
		 
$database->setQuery($query);
$results = $database->loadObjectList();

if ($database->getErrorMsg()) echo $database->getErrorMsg();
 
while (ob_get_level()) {
   ob_end_clean();
}

// start output buffering
if (ob_get_length() === false) {
   ob_start();
}

echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"."\n";
?>
<rss version="2.0">
<channel>
<title><?php echo htmlspecialchars(mb_convert_encoding($mosConfig_sitename, "UTF-8", "CP1251"));?></title>
<link><?php echo htmlspecialchars($mosConfig_live_site); ?></link>
<description><?php echo htmlspecialchars(mb_convert_encoding($mosConfig_MetaDesc, "UTF-8", "CP1251")); ?></description>
<language>ru</language>
<webMaster>marco.manti@gmail.com</webMaster>
<generator>marco_manti rss generator</generator>
<image>
	<url><?php echo $mosConfig_live_site."/templates/mnt_frontpage/images/logo_small.png"; ?></url>
	<title><?php echo htmlspecialchars(mb_convert_encoding($mosConfig_sitename, "UTF-8", "CP1251")); ?></title>
	<link><?php echo $mosConfig_live_site; ?></link>
</image>
<?php
foreach($results as $res) {
	$title = htmlspecialchars(mb_convert_encoding($res->title, "UTF-8", "CP1251"),ENT_QUOTES,"UTF-8");
	$link = htmlspecialchars(sefRelToAbs(ampReplace("index.php?option=com_estateagent&act=object&task=showEO&Itemid=14&id=".$res->id)),ENT_QUOTES,"UTF-8");
	
	$descr = mb_convert_encoding($res->description, "UTF-8", "CP1251");
	$descr = preg_replace("'<script[^>]*>.*?</script>'si","",$descr);
	$descr = preg_replace('/<a\s+.*?href="([^"]+)"[^>]*>([^<]+)<\/a>/is','\2 (\1)', $descr);
	$descr = preg_replace('/<!--.+?-->/','',$descr);
	$descr = preg_replace('/{.+?}/','',$descr);
	$descr = preg_replace('/&nbsp;/',' ',$descr);
	$descr = preg_replace('/&amp;/',' ',$descr);
	$descr = preg_replace('/&quot;/',' ',$descr);
	$descr = strip_tags($descr);
	$description = htmlspecialchars($descr);

	$pubdate = date('r', strtotime($res->timestamp));
	?>
	<item>
		<title><?php echo $title; ?></title>
   	<link><?php echo $link; ?></link>
   	<description><?php echo $description; ?></description>
      <guid><?php echo sprintf('%05d', $res->id); ?></guid>
      <pubDate><?php echo $pubdate; ?></pubDate>
    </item>
	<?php
} ?>
</channel>
</rss>
<?php
 ob_end_flush();
 die();
?>